package com.demo2do.fms.admin.web.controller.data.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.business.Province;

/**
 * 查询省份代码
 *
 * @author Chang Liu
 * 
 */
@Controller
@RequestMapping("/provinces")
@MenuSetting("menu-data")
public class ProvinceController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Province> provinceService;
    
    /**
     * list province
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-province")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(Page page){
        
        ModelAndView modelAndView = new ModelAndView("data/business/province/province-list");
        
        int totals = provinceService.count(Province.class);
        if(totals > 0){
            
            List<Province> provinces = provinceService.listByPage(Province.class, page.with(totals));
            
            modelAndView.addObject("provinces", provinces);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 输出当前页记录 / 输出所有记录
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/province-export.xlsx"));

        // 1. list province
        int totals = provinceService.count(Province.class);
        if(totals > 0){
            List<Province> provinces = provinceService.listByPage(Province.class, page.with(totals));
            modelAndView.addObject("provinces", provinces);
        }
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}